package com.link.base.wechat.wxsubscriber.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.link.base.wechat.basic.util.WxExceptionUtil;
import com.link.base.wechat.wxsubscriber.model.OpenIdList;
import com.link.base.wechat.wxsubscriber.model.UserOpenIdList;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.HttpRequestUtil;
import com.link.core.util.JsonUtil;
import com.link.core.util.StringUtils;

/**
 * 微信公众号用户管理工具类
 *
 * @author ouyangzhenqiang
 * @date 2019/03/01 11:07
 */
public class WxSubscriberUtil {

    /**
     * 拉取公众号全部用户openId
     *
     * @author ouyangzhenqiang
     * @date 2019/03/01 10:51
     * @param access_token 接口调用凭证
     */
    public static OpenIdList getAllOpenIdList(String access_token) throws Exception {
        if (StringUtils.isBlank(access_token)) {
            throw new ServiceException("WXSUB-003");
        }
        OpenIdList openIdList = new OpenIdList();
        Integer total;
        Integer count;
        JSONObject subscriberPage = getOpenIdPage(access_token, "");
        total = subscriberPage.getInteger("total");
        openIdList.setTotal(String.valueOf(total));
        count = subscriberPage.getInteger("count");
        parseOpenId(subscriberPage, openIdList);
        while (total > count) {
            String next_open_id = subscriberPage.getString("next_openid");
            subscriberPage = getOpenIdPage(access_token, next_open_id);
            count = count + subscriberPage.getInteger("count");
            parseOpenId(subscriberPage, openIdList);
        }
        return openIdList;
    }

    /**
     * 受限于单次拉取用户最多数量为10000，该方法是直接拉取，判断是否需要循环调用由getAllSubscriberList方法判断
     *
     * @author ouyangzhenqiang
     * @date 2019/03/01 10:54
     * @param access_token 接口调用凭证
     * @param next_open_id 下一个openId
     */
    private static JSONObject getOpenIdPage(String access_token, String next_open_id) throws Exception {
        if (StringUtils.isBlank(access_token)) {
            throw new ServiceException("WXSUB-003");
        }
        String url = "https://api.weixin.qq.com/cgi-bin/user/get";
        String param = "access_token=" + access_token + "&next_openid=" + next_open_id;
        String resultStr = HttpRequestUtil.sendGet(url, param);
        return WxExceptionUtil.parse(resultStr);
    }

    /**
     * 解析公众号用户查询对象
     *
     * @author ouyangzhenqiang
     * @date 2019/03/01 11:59
     * @param obj openId对象
     */
    private static void parseOpenId(JSONObject obj, OpenIdList openIdList) {
        JSONArray openIdArray = obj.getJSONObject("data").getJSONArray("openid");
        for (int i = 0; i < openIdArray.size(); i++) {
            openIdList.getOpenList().add(openIdArray.getString(i));
        }
    }

    /**
     * 获取用户基本信息
     *
     * @author ouyangzhenqiang
     * @date 2019/02/27 15:18
     * @param access_token 接口调用凭证
     * @param openId openId
     */
    public static JSONObject getSubscriberInfoByOpenId(String access_token, String openId) throws Exception {
        if (StringUtils.isBlank(access_token)) {
            throw new ServiceException("WXSUB-003");
        }
        if (StringUtils.isBlank(openId)) {
            throw new ServiceException("WXSUB-002");
        }
        String url = "https://api.weixin.qq.com/cgi-bin/user/info";
        String param = "access_token=" + access_token + "&openid=" + openId + "&lang=zh_CN";
        String resultStr = HttpRequestUtil.sendGet(url, param);
        return WxExceptionUtil.parse(resultStr);
    }

    /**
     * 获取用户基本信息
     *
     * @author 唐健
     * @date 2020/07/01
     * @param access_token 接口调用凭证
     * @param userOpenIdList openId list
     */
    public static JSONObject getSubscriberInfoListByOpenId(String access_token, UserOpenIdList userOpenIdList) throws Exception {
        if (StringUtils.isBlank(access_token)) {
            throw new ServiceException("WXSUB-003");
        }
        String content = JsonUtil.toString(userOpenIdList);
        if (StringUtils.isBlank(content)) {
            throw new ServiceException("WXSUB-002");
        }
        String url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=" + access_token;
        String resultStr = HttpRequestUtil.sendPost(url, content,null);
        return WxExceptionUtil.parse(resultStr);
    }

    /**
     * 设置用户备注名
     *
     * @author ouyangzhenqiang
     * @date 2019/02/27 15:18
     * @param access_token 接口调用凭证
     * @param openId       用户标识
     * @param remark       新的备注名，长度必须小于30字符
     */
    public static JSONObject updateRemark(String access_token, String openId, String remark) throws Exception {
        if (StringUtils.isBlank(access_token)) {
            throw new ServiceException("WXSUB-003");
        }
        if (StringUtils.isBlank(openId)) {
            throw new ServiceException("WXSUB-002");
        }
        if (StringUtils.isBlank(remark)) {
            throw new ServiceException("WXSUB-004");
        }
        String url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=" + access_token;
        JSONObject obj = new JSONObject();
        obj.put("openid", openId);
        obj.put("remark", remark);
        String content = obj.toJSONString();
        String resultStr = HttpRequestUtil.sendPost(url, content, null);
        return WxExceptionUtil.parse(resultStr);
    }

}
